package com.wechat.command;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 命令 - 执行结果
 * 
 * @author DELL
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
public class CommandResult {

	public static String Return_Success = "SUCCESS";
	public static String Return_Err = "FAIL";

	public static String Return_Code_Success = "0";
	public static String Return_Code_Err = "1";

	public static CommandResult SUCCESS = new CommandResult().setReturn_msg(Return_Success)
			.setReturn_code(Return_Code_Success);

	protected String return_code;
	protected String return_msg;
	protected String err_code;
	protected String err_code_des;
	protected Object result;

	public boolean isSuccess() {
		return Return_Code_Success.equals(this.return_code);
	}

	@SuppressWarnings("unchecked")
	public <T> T result2Object() {
		return (T) result;
	}

	public static CommandResult fail(Object result) {
		return new CommandResult().setReturn_msg(Return_Err).setReturn_code(Return_Code_Err).setResult(result);
	}

	public static CommandResult success(Object result) {
		return new CommandResult().setReturn_msg(Return_Success).setReturn_code(Return_Code_Success).setResult(result);
	}

	public static CommandResult success() {
		return new CommandResult().setReturn_msg(Return_Success).setReturn_code(Return_Code_Success);
	}

	public static CommandResult wrap(Object result) {
		if (result != null && result instanceof CommandResult) {
			return (CommandResult) result;
		}
		return CommandResult.success(result);
	}
}
